﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Secams.controller;
using System.IO;

namespace Secams.form
{
    public partial class FormConfig : Form
    {
        public FormConfig()
        {
            InitializeComponent();

            this.txtDbFile.Text = AppContext.getInstance().dbFilePath;
        }


        private void FormConfig_Load(object sender, EventArgs e)
        {
            TextReader scenarioXmlData = new StreamReader(AppContext.getInstance().getScenarioFilePath("scenario-collections.xml"));

            this.txtScoreHit.Text = AppContext.getInstance().getProperty("score-hit", 1).ToString();
            this.txtScoreMiss.Text = AppContext.getInstance().getProperty("score-miss", -2).ToString();
            this.txtScoreFa.Text = AppContext.getInstance().getProperty("score-fa", -1).ToString();

            this.txtDefaultDelay.Text = AppContext.getInstance().getProperty("event-default-delay", 1).ToString();

            this.txtScenarioSource.Text = scenarioXmlData.ReadToEnd();
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            AppContext.getInstance().setProperty("score-hit", int.Parse(this.txtScoreHit.Text));
            AppContext.getInstance().setProperty("score-miss", int.Parse(this.txtScoreMiss.Text));
            AppContext.getInstance().setProperty("score-fa", int.Parse(this.txtScoreFa.Text));
            AppContext.getInstance().setProperty("score-cr", int.Parse(this.txtScoreCr.Text));

            AppContext.getInstance().setProperty("event-default-delay", int.Parse(this.txtDefaultDelay.Text));

        }

        private void createTables()
        {
            // This one not used: using XML instead
            //ConfigDao configDao = new ConfigDao(null);
            //configDao.initialize();

            ParticipantDao participantDao = new ParticipantDao(AppContext.getInstance().dbFilePath);
            if (participantDao.initialize())
                log("Participant table created.");

            ActionTraceDao actionTraceDao = new ActionTraceDao(AppContext.getInstance().dbFilePath);
            if (actionTraceDao.initialize())
                log("ActionTrace table created.");

            EventTraceDao eventTraceDao = new EventTraceDao(AppContext.getInstance().dbFilePath);
            if (eventTraceDao.initialize())
                log("EventTrace table created.");
        }

        private void btnDbCreateTables_Click(object sender, EventArgs e)
        {
            createTables();
        }

        private void btnDbInit_Click(object sender, EventArgs e)
        {
            string emptyDb = AppContext.getInstance().getDataFilePath("EmptyDB.mdb");
            string oldDb = AppContext.getInstance().dbFilePath;

            int dotPos = AppContext.getInstance().dbFilePath.LastIndexOf(".");
            string backupDb = AppContext.getInstance().dbFilePath.Substring(0, dotPos);
            backupDb += "_" + DateTime.Now.ToString("yyMMdd")+".mdb";

            try
            {
                if (File.Exists(backupDb))
                    File.Delete(backupDb);

                // Make a backup of current file
                File.Move(oldDb, backupDb);
                log("Backup Database " + backupDb + " created.");

                // Copy the empty db as new DB
                File.Copy(emptyDb, oldDb);
                createTables();
                log("Database file Initialized."+Environment.NewLine);
            }
            catch (Exception ex)
            {
                log("Exception while initializing data."+ ex.Message);
            }


        }

        private void btnDbExport_Click(object sender, EventArgs e)
        {
            SaveFileDialog fileDlg = new SaveFileDialog();
            fileDlg.Filter = "MDB Files |*.mdb";
            fileDlg.Title = "Export as File";

            if (fileDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string currDb = AppContext.getInstance().dbFilePath;
                FileInfo currFile = new FileInfo(currDb);
                FileInfo newFile = new FileInfo(fileDlg.FileName);
                if (currFile.Equals(newFile))
                {
                    log("Select different destination");
                }
                else
                {
                    File.Copy(currDb, fileDlg.FileName);
                    log("File copied to " + fileDlg.FileName);
                }
            }
        }

        private void log(string message)
        {
            txtLog.AppendText(message + Environment.NewLine);
        }
    }
}
